// login/login.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
  },

  /**
   * 组件的初始数据
   */
  data: {
    show: false,
    isbtn:true,
    isph:true,
    canIUseGetUserProfile: false,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    showClose() {
      this.setData({
        show: false
      });
    },
    loginClose() {
      this.hideLogin()
    },
    getUserProfile(e) {
      wx.getUserProfile({
        lang:'zh_CN',
        desc:'用于完善会员资料',
        success: (res) => {
          this.getAuth(res);
        },
        fail:error =>{
          console.log(error)
        }
      })
    },
    //登录
    getAuth: function(userInfo) {
      console.log('userInfo',userInfo)
      var that = this;
      if(that.data.isbtn){
        that.setData({
          isbtn:false
        })
      wx.login({
        fail: function(res) {
          wx.showToast({
            title: '登录失败',
            icon: 'none',
            duration: 2000
          })
          that.setData({
            isbtn: true
          })
        },
        success: function(res) {
          console.log('wx.login',res);
          wx.setStorageSync('loginCode', res.code);
          if(that.data.canIUseGetUserProfile){
            that.saveUserInfo1(res.code,userInfo);
          }else{
            that.saveUserInfo1(res.code,userInfo.detail);
          }
        }
      });
      }
    },

    //保存用户信息
    // saveUserInfo: function(userCode) {
    //   var that = this;
    //   wx.getUserInfo({
    //     lang: 'zh_CN',
    //     fail: function(res) {
    //       wx.showToast({
    //         title: '授权失败',
    //         icon: 'none',
    //         duration: 2000
    //       })
    //       that.setData({
    //         isbtn: true
    //       })
    //     },
    //     success: function(res) {
    //       console.log('getUserInfo',res)
    //       app.req({
    //         url: 'orgmini/user/authorize',
    //         data: {
    //           encryptedData: res.encryptedData,
    //           iv: res.iv,
    //           code: userCode
    //         },
    //         method: 'POST',
    //         success: function(res) {
    //           if (res.code === 200) {
    //              wx.setStorageSync('uid', res.data.uid);
    //             that.setData({
    //               uid: res.data.uid,
    //               isbtn: true
    //             })
    //             that.getUserPhone();
    //           } else {
    //             that.setData({
    //               isbtn: true
    //             })
    //             wx.showToast({
    //               title: res.msg,
    //               icon: 'none',
    //               duration: 2000
    //             })
    //           }
    //         }
    //       });

    //     }
    //   });
    // },
    //保存用户信息
    saveUserInfo1: function(userCode,userInfo) {
      var that = this;
      app.req({
        url: 'orgmini/user/authorize',
        data: {
          encryptedData: userInfo.encryptedData,
          iv: userInfo.iv,
          code: userCode
        },
        method: 'POST',
        success: function(res) {
          if (res.code === 200) {
             wx.setStorageSync('uid', res.data.uid);
            that.setData({
              uid: res.data.uid,
              isbtn: true
            })
            that.getUserPhone();
          } else {
            that.setData({
              isbtn: true
            })
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 2000
            })
          }
        }
      });
    },
    //查看用户是否有手机号
    getUserPhone: function() {
      var that = this;
      that.setData({
        show: true
      });
    },
    //需要用户点击，获取微信用户手机号
    getUserPhoneByWeChat: function(e) {
      console.log('手机号',e);
      var that = this;
      if (that.data.isph){
        var detail = e.detail;
        // var uid = wx.getStorageSync("uid");
        var uid = that.data.uid
        var data = {
          encryptedData: detail.encryptedData,
          ivdata: detail.iv,
          uid: uid
        }
        wx.login({
          fail: function (res) {
            wx.showToast({
              title: '登录失败',
              icon: 'none',
              duration: 2000
            })
            that.setData({
              isph: true
            })
          },
          success: function (res) {
            data.code = res.code
            if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
              that.setData({
                modalstatus: true,
                isph: true
              })
            } else {
              // data.code = code;
              that.decryptPhone(data);
              console.log('授权', data)
            }
            // wx.setStorageSync('loginCode', res.code);
            // data.code = res.code;
            // that.decryptPhone(data)
          }
        });
      }
     },
    //解密手机号数据
    decryptPhone: function(data) {
      var that = this
      app.req({
        url: 'orgmini/user/deciphering?uid=' + that.data.uid,
        data: data,
        method: 'POST',
        success: function(res) {
          if (res.code === 200) {
            if(res.data.uid){
              wx.setStorageSync('uid', res.data.uid);
            }
            wx.setStorageSync('phone', res.data.telPhone);
            that.setData({
              show: false,
              isph: true
            });
            that.hideLogin()
          } else {
            that.setData({
              isph: true
            })
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 2000
            })
          }
        }
      });
    },
    // 传递给父组件
    hideLogin: function(e) {
      var that = this;
      var myEventDetail = {
        loginShow: false,
        type: '取消登录框'
      } // detail对象，提供给事件监听函数
      this.triggerEvent('myevent', myEventDetail) //myevent自定义名称事件，父组件中使用
    }
  },
  ready:function(options) {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
  },
})